// pages/gooddelait/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buynum: 1,
    value:'',
    pagedata: '',
    show: false,
    shownum: false,
    actions: [{
        name: '选项',
      },
      {
        name: '选项',
      },
      {
        name: '选项',
        subname: '描述信息',
        openType: 'share',
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options.id);
    //请求数据列表
    wx.request({
      url: app.globalData.url + '/zkxj/good/detail',
      data: {
        "goodId": options.id,
      },
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res.data.data);
        this.setData({
          pagedata: res.data.data
        })
      }
    })
  },
  onChange(event){
    this.setData({
      buynum:event.detail
    })
  },
 
  onClose() {
    this.setData({
      show: false
    });
  },
  onClosenu() {
    this.setData({
      shownum: false
    });
  },
  handledebuynu() {
    if(this.data.buynum>1){

      this.setData({
        buynum : this.data.buynum-1
      })
    }
    console.log(this.data.buynum);
  },
  handleopennu(){
    this.setData({
      shownum: true
    });
  },
  handleaddbuynu(){
    this.setData({
      buynum : this.data.buynum+1
    })
  },
  onSelect(event) {
    console.log(event.detail);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  tocart() {
    wx.navigateTo({
      url: `/pages/jingjicart/index`
    })
  },
  handleclose(){
    this.setData({
      show: false
    });
  },
  toaddcarts(){
    this.setData({
      show: true
    });
  },
  tobuys(){
    this.setData({
      show: true
    });
  },
  toaddcart() {
    this.setData({
      show: false
    });
    var arr = [{
      "coverPic": '',
      "id": "",
      "name": "",
      "num": 0,
      "oldpric": 0,
      "price": 0
    }]
    arr[0].coverPic = this.data.pagedata.coverPic
    arr[0].id = this.data.pagedata.id
    arr[0].name = this.data.pagedata.name
    arr[0].num = this.data.buynum
    arr[0].oldpric = this.data.pagedata.originalPrice
    arr[0].price = this.data.pagedata.price
    console.log(arr);
    wx.request({
      url: app.globalData.url + '/zkxj/cart/area/addNewGood',
      data: {
        "level": wx.getStorageSync('loginuser').level,
        "goodAreaDetail": 2,
        "goodId": this.data.pagedata.id,
        "modifiedtime": "",
        "num": this.data.buynum,
        "oldPrice": this.data.pagedata.originalPrice,
        "price": this.data.pagedata.price,
        "userId": wx.getStorageSync('userId')
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res);
        if (res.data.msg == "商品添加成功！") {
          wx.showToast({
            title: '添加成功',
            icon: 'success',
            duration: 2000,
            mask: true
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'error',
            duration: 2000,
            mask: true
          })
        }
      },
    })
  },
  tobuy() {
    this.setData({
      show: false
    });
    var arr = [{
      "coverPic": '',
      "id": "",
      "name": "",
      "num": 0,
      "oldpric": 0,
      "price": 0
    }]
    arr[0].coverPic = this.data.pagedata.coverPic
    arr[0].id = this.data.pagedata.id
    arr[0].name = this.data.pagedata.name
    arr[0].num = this.data.buynum
    arr[0].oldpric = this.data.pagedata.originalPrice
    arr[0].price = this.data.pagedata.price
    console.log(arr);
    var detailsData = JSON.stringify(arr);
    detailsData = encodeURIComponent(detailsData);
    wx.navigateTo({
      url: `/pages/zdseepg/index?arr=${detailsData}&sum=${JSON.stringify(this.data.pagedata.price*this.data.buynum)}&type=1&upgradeLevel=0`
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})